//
//  RegisterMobileViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-8-24.
//
//

#import "RegisterMobileViewController.h"
#import "RegisterDetailViewController.h"
#import "LoginViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface RegisterMobileViewController ()

@end

@implementation RegisterMobileViewController
{
    UIAlertView *_baseAlert;
    NSUserDefaults *_userDefaults;
    
    UIImageView *_imgview;
    UIButton *_genpasBtn;
    UITextField *_passwordTF;
    UILabel *_descLbl;
    UIButton *_registerBtn;
    
    NSString *_checkCode;
}

@synthesize mobile;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //初始化视图
    _checkCode = [[NSString alloc]init];
    [self init_view];
}

- (void)dealloc
{
    [_imgview release];
    [_genpasBtn release];
    [_passwordTF release];
    [_descLbl release];
    [_registerBtn release];
    [_registerBtn release];
    [_checkCode release];
    
    _imgview = nil;
    _genpasBtn = nil;
    _passwordTF = nil;
    _descLbl = nil;
    _registerBtn = nil;
    _checkCode = nil;
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




//初始化视图
- (void)init_view
{
    //定义title
    self.title = @"登录/注册";
    //加载背影
    [self.view setBackgroundColor:BackgroundColor];

    
    //头像显示
    _imgview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
    _imgview.frame = CGRectMake(120, ADJUST_IOS7STATUES_HIGHT+80, 80, 80);
    [self.view addSubview:_imgview];
    
    //生成手机验证码
    _genpasBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+170, 280, 40)];
    [_genpasBtn setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    [_genpasBtn setBackgroundColor:[UIColor greenColor]];
    [_genpasBtn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    _genpasBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _genpasBtn.layer.borderColor = [UIColor grayColor].CGColor;
    _genpasBtn.layer.borderWidth = 1 ;
    _genpasBtn.layer.cornerRadius = 10 ;
    [self.view addSubview:_genpasBtn];
    [_genpasBtn addTarget:self action:@selector(genpassEvent) forControlEvents:UIControlEventTouchUpInside];
    
    //密码
    _passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+220, 280, 50)];
    _passwordTF.placeholder = [NSString stringWithFormat:@"输入手机验证码"];
    _passwordTF.keyboardType = UIKeyboardTypeNumberPad;
    [_passwordTF setBorderStyle:UITextBorderStyleBezel];
    _passwordTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _passwordTF.borderStyle = UITextBorderStyleRoundedRect;
    _passwordTF.delegate = self ;
    _passwordTF.returnKeyType =UIReturnKeyDone;
    _passwordTF.layer.borderColor = [UIColor grayColor].CGColor;
    _passwordTF.layer.borderWidth = 1 ;
    _passwordTF.layer.cornerRadius = 10 ;
    [self.view addSubview:_passwordTF];
    
    //说明
    _descLbl = [[UILabel alloc]initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+280, 280, 20)];
    _descLbl.text = @"请确认手机号码无误，注册成功后将无法修改";
    _descLbl.font = [UIFont systemFontOfSize:20];
    _descLbl.adjustsFontSizeToFitWidth = YES;
    _descLbl.textColor = [UIColor grayColor];
    _descLbl.backgroundColor = [UIColor clearColor];
    if (self.type == 1) {
        [self.view addSubview:_descLbl];
    }
    
    //验证手机验证码
    _registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+310, 280, 40)];
    [_registerBtn setTitle:@"下一步" forState:UIControlStateNormal];
    _registerBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [_registerBtn setBackgroundColor:[UIColor redColor]];
    [_registerBtn setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
    _registerBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _registerBtn.layer.borderColor = [UIColor grayColor].CGColor;
    _registerBtn.layer.borderWidth = 1 ;
    _registerBtn.layer.cornerRadius = 10 ;
    [self.view addSubview:_registerBtn];
    [_registerBtn addTarget:self action:@selector(registerEvent) forControlEvents:UIControlEventTouchUpInside];

}


//获取手机验证码
- (void)genpassEvent
{
    AccountManager *accountmanager = [AccountManager shareManager];
    accountmanager.delegate = self;
    
    if (self.type == 1) {
        [accountmanager registerSendVerifyCode:self.mobile];
    } else {
        [accountmanager getPassBySendSMSCode:self.mobile];
    }
}

//注册
- (void)registerEvent
{
    //检测密码是否为空
    if ([_passwordTF.text isEqualToString:@""] == YES) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"注册出错" message:@"手机号验证码不能为空"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
    }
    
    //成功注册
    else if ([_checkCode isEqualToString:_passwordTF.text]){
        RegisterDetailViewController *registerController = [[RegisterDetailViewController alloc] init];
        registerController.mobile = self.mobile;
        registerController.type = self.type;
        [self.navigationController pushViewController:registerController animated:YES];
        [registerController release];
    }
    
    else{
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"验证码出错" message:@"请填写正确的验证码"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
    }
}


- (void) performDismiss: (NSTimer *)timer {
    [_baseAlert dismissWithClickedButtonIndex:0 animated:NO];//important
    [_baseAlert release];
    _baseAlert = NULL;
}


//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}


//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}


#pragma mark UITextFieldDelegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_passwordTF resignFirstResponder];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


//请求验证码
- (void)sendSMSCode:(BOOL)isSuccess
          checkCode:(NSString*)checkCode
{
    if (isSuccess) {
        _checkCode = checkCode;
    } else {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"获取验证码出错" message:@"请换一个手机号码试试"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
    }
    
}

//找回密码
- (void)getPassWd:(BOOL)isSuccess
{
    if (isSuccess) {
        LoginViewController *loginViewController=[[LoginViewController alloc] init];
        [self.navigationController pushViewController:loginViewController animated:YES];
        [loginViewController release];
    }
    else
    {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"获取临时密码出错" message:@"请重新获取"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
    }
    
}

@end
